package com.leetcode.题库;

/**
 * @author 覃国强
 * @date 2022-03-03 20:13
 */
public class 题库_0912_排序数组 {


  static
  class Solution {
    public int[] sortArray(int[] nums) {
      for (int left = 0, right = nums.length - 1; left < right; ++left, --right) {
        int minIndex = left;
        int maxIndex = right;
        for (int i = left; i <= right; ++i) {
          minIndex = nums[i] < nums[minIndex] ? i : minIndex;
          maxIndex = nums[i] > nums[maxIndex] ? i : maxIndex;
        }
        if (minIndex == maxIndex) {
          return nums;
        }
        swap(nums, left, minIndex);
        if (maxIndex == left) {
          maxIndex = minIndex;
        }
        swap(nums, right, maxIndex);
      }
      return nums;
    }

    private void swap(int[] nums, int i, int j) {
      int temp = nums[i];
      nums[i] = nums[j];
      nums[j] = temp;
    }
  }

}
